package main

import (
  "gorm.io/gorm"
  "gorm.io/driver/mysql"
  "log"
  "fmt"
)

type Product struct {
  //gorm.Model
  ID uint `gorm:"primaryKey"`
  Code  string
  Price uint `gorm:"index"`
}

func main() {
  log.Println(fmt.Sprintf("dddddddddd\n"));
  dsn := "user:password@tcp(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=True&loc=Local"
  db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
  if err != nil {
    panic("failed to connect database")
  }
  db = db.Debug()
  db.AutoMigrate(&Product{})
  db.Where("1=1").Delete(&Product{});
  db.Create(&Product{Code: "D41", Price: 101})
  db.Create(&Product{Code: "D42", Price: 102})
  db.Create(&Product{Code: "D43", Price: 103})
  db.Create(&Product{Code: "D44", Price: 104})
  var product Product
  db.First(&product, 1) // find product with integer primary key
  db.First(&product, "code = ?", "D41") // find product with code D41
  db.Model(&product).Update("Price", 201)

  db.Model(&Product{}).Where("Code = ?", "D42").Update("Price", 202)
  db.Model(&Product{}).Where("Code = ?", "D43").Updates(Product{Price: 203, Code: "F42"}) // non-zero fields
  db.Model(&Product{}).Where("Code = ?", "D44").Updates(map[string]interface{}{"Price": 204, "Code": "F42"})
  //db.Delete(&product, 1)
}
